package com.blah.gwtgames.client.mines;

import com.blah.gwtgames.client.common.ui.Slider;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.client.ui.ChangeListener;

public class SettingsPanel extends Composite implements ChangeListener{
	private Slider mineCountSlider = new Slider(0, 99);
	private Slider widthSlider = new Slider(5, 50);
	private Slider heightSlider = new Slider(5, 25);
	
	private Label mineCountLabel = new Label();
	private Label widthLabel = new Label();
	private Label heightLabel = new Label();
	
	private MineGame mineGame;
	
	public SettingsPanel(MineGame mineGame)
	{
		this.mineGame = mineGame;
		
		VerticalPanel vPanel = new VerticalPanel();
		HorizontalPanel hPanel = new HorizontalPanel();
		
		hPanel.add(new Label("Mine count: "));
		hPanel.add(mineCountSlider);
		hPanel.add(mineCountLabel);
		vPanel.add(hPanel);
		
		hPanel = new HorizontalPanel();
		hPanel.add(new Label("Width: "));
		hPanel.add(widthSlider);
		hPanel.add(widthLabel);
		vPanel.add(hPanel);
		
		hPanel = new HorizontalPanel();
		hPanel.add(new Label("Height: "));
		hPanel.add(heightSlider);
		hPanel.add(heightLabel);
		vPanel.add(hPanel);
		
		mineCountSlider.addChangeListener(this);
		widthSlider.addChangeListener(this);
		heightSlider.addChangeListener(this);
		
		Button button = new Button("Go!");
		button.addClickListener(new ClickListener()
			{
				final SettingsPanel settingsDialog = SettingsPanel.this;
			
				public void onClick(Widget sender)
				{
					settingsDialog.startGame();
				}
			}
		);
		vPanel.add(button);
		
		initWidget(vPanel);
	}
	
	public void display(int width, int height, int mineCount)
	{
		setVisible(true);
		
		mineCountSlider.setValue(mineCount);
		widthSlider.setValue(width);
		heightSlider.setValue(height);
	}
	
	private void startGame()
	{
		int width = widthSlider.getValue();
		int height = heightSlider.getValue();
		int mineCount = mineCountSlider.getValue();
		
		mineGame.newGame(width, height, mineCount);
		
		setVisible(false);
	}

	public void onChange(Widget sender)
	{
		boolean gridSizeChanged = false;
		
		if (sender == mineCountSlider)
		{
			mineCountLabel.setText("" + mineCountSlider.getValue());
		}
		else if (sender == widthSlider)
		{
			widthLabel.setText("" + widthSlider.getValue());
			gridSizeChanged = true;
		}
		else if (sender == heightSlider)
		{
			heightLabel.setText("" + heightSlider.getValue());
			gridSizeChanged = true;
		}
		
		if (gridSizeChanged)
		{
			int tileCount = widthSlider.getValue() * heightSlider.getValue();
			mineCountSlider.setScale((int)(tileCount * 0.05), (int)(tileCount * 0.30));
		}
	}
}
